home *** CD-ROM | disk | FTP | other *** search
/ Complete Linux / Complete Linux.iso / docs / apps / database / postgres / postgre4.z / postgre4 / src / contrib / pgperl / pg-mus < prev    next >
Encoding:
Text File  |  1992-08-27  |  3.4 KB  |  162 lines

  1. #!/usr/local/bin/perl
  2. # $Header: /private/postgres/src/contrib/pgperl/RCS/pg-mus,v 1.2 1991/03/08 13:22:34 kemnitz Exp $
  3. # This perl-script take a "mus" file and converts it to C.
  4. # Written by Larry Wall (?).
  5. # Adapted for use with Postgres by Igor Metz <metz@iam.unibe.ch>
  6.  
  7. # $Id: pg-mus,v 1.2 1991/03/08 13:22:34 kemnitz Exp $
  8. # $Log: pg-mus,v $
  9. # Revision 1.2  1991/03/08  13:22:34  kemnitz
  10. # added RCS header.
  11. #
  12. # Revision 1.1  90/10/24  20:31:14  cimarron
  13. # Initial revision
  14. # Revision 1.2  90/08/23  14:17:39  metz
  15. # o comments added
  16. # Revision 1.1  90/08/23  11:41:19  metz
  17. # Initial revision
  18.  
  19. while (<>) {
  20.     if (s/^CASE\s+//) {
  21.     @fields = split;
  22.     $funcname = pop(@fields);
  23.     $rettype = "@fields";
  24.     @modes = ();
  25.     @types = ();
  26.     @names = ();
  27.     @outies = ();
  28.     @callnames = ();
  29.     $pre = "\n";
  30.     $post = '';
  31.  
  32.     while (<>) {
  33.         last unless /^[IO]+\s/;
  34.         @fields = split(' ');
  35.         push(@modes, shift(@fields));
  36.         push(@names, pop(@fields));
  37.         push(@types, "@fields");
  38.     }
  39.     while (s/^<\s//) {
  40.         $pre .= "\t    $_";
  41.         $_ = <>;
  42.     }
  43.     while (s/^>\s//) {
  44.         $post .= "\t    $_";
  45.         $_ = <>;
  46.     }
  47.     $items = @names;
  48.     $namelist = '$' . join(', $', @names);
  49.     $namelist = '' if $namelist eq '$';
  50.     print <<EOF;
  51.     case US_$funcname:
  52.     if (items != $items)
  53.         fatal("Usage: &$funcname($namelist)");
  54.     else {
  55. EOF
  56.     if ($rettype eq 'void') {
  57.         print <<EOF;
  58.         /* int retval = 1; */
  59. EOF
  60.     }
  61.     else {
  62.         print <<EOF;
  63.         $rettype retval;
  64. EOF
  65.     }
  66.     foreach $i (1..@names) {
  67.         $mode = $modes[$i-1];
  68.         $type = $types[$i-1];
  69.         $name = $names[$i-1];
  70.         $what = ($type =~ /^(struct\s+\w+|char|\w+)\s*\*$/ ? "get" : "gnum");
  71.         $type .= "\t" if length($type) < 4;
  72.         $cast .= "\t" if length($cast) < 8;
  73.         $x = "\t" x (length($name) < 6);
  74.         if ($mode =~ /O/) {
  75.         if ($what eq 'gnum') {
  76.             push(@outies, "\t    str_numset(st[$i], (double) $name);\n");
  77.         }
  78.         else {
  79.             push(@outies, "\t    str_set(st[$i], (char*) $name);\n");
  80.         }
  81.         push(@callnames, "&$name");
  82.         }
  83.         else {
  84.         push(@callnames, $name);
  85.         }
  86.         if ($mode =~ /I/) {
  87.             if ($type =~ /^char\*$/) {
  88.               # no special handling necessary
  89.           print <<EOF;
  90.         $type    $name =$x    str_get(st[$i]);
  91. EOF
  92.           }
  93.           elsif ($type =~ /^\w+\*$/) {
  94.           print <<EOF;
  95.         $type    $name =$x ($type) dbl2uint(str_gnum(st[$i]));
  96. EOF
  97.           }
  98.           else {
  99.           print <<EOF;
  100.         $type    $name =$x ($type) dbl2uint(str_gnum(st[$i]));
  101. EOF
  102.           }
  103.            }
  104.     }
  105.     $callnames = join(', ', @callnames);
  106.     $outies = join("\n",@outies);
  107.     if ($rettype eq 'void') {
  108.         print <<EOF;
  109. $pre        (void)$funcname($callnames);
  110. EOF
  111.     }
  112.     else {
  113.         print <<EOF;
  114. $pre        retval = $funcname($callnames);
  115. EOF
  116.     }
  117.  
  118.     if ($rettype =~ /^char\s*\*$/) {  # char*
  119.         print <<EOF;
  120.         str_set(st[0], retval);
  121. EOF
  122.         }
  123.     elsif ($rettype =~ /^\s*void\s*$/) { # void
  124.         print <<EOF;
  125.         str_numset(st[0], 1.0);
  126. EOF
  127.     }
  128.     elsif ($rettype =~ /^\w+\s*\*+$/) { # anyothertype*
  129.         print <<EOF;
  130.         str_numset(st[0],  uint2dbl((unsigned int) retval));
  131. EOF
  132.         }
  133.     elsif ($rettype =~ /^(char|short|int|unsigned\s+int|signed\s+int)$/) {
  134.         print <<EOF;
  135.         str_numset(st[0],  uint2dbl((unsigned int) retval));
  136. EOF
  137.     }
  138.     else { # ($rettype =~ /^\w+\s*$/) 
  139.         print <<EOF;
  140.         str_nset(st[0], (char*) &retval, sizeof(retval));
  141. EOF
  142.     }
  143.     print $outies if $outies;
  144.     print $post if $post;
  145.     if (/^END/) {
  146.         print "\t}\n\treturn sp;\n";
  147.     }
  148.     else {
  149.         redo;
  150.     }
  151.     }
  152.     elsif (/^END/) {
  153.     print "\t}\n\treturn sp;\n";
  154.     }
  155.     else {
  156.     print;
  157.     }
  158. }
  159.